﻿using System;
using System.Windows.Data;
using MVVM.Northwind.Employee.Commands;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Collections.Generic;

namespace MVVM.Northwind.Employee.ViewModel
{
    public class MainWindowViewModel:WorkspaceViewModel
    {
        // The Employee Window ViewModel will have
        // a collection of Emplyee List View Models. This collection
        // of view models can be bound to the item template
        // of main window
        private ObservableCollection<WorkspaceViewModel> _workSpaces;
        private ReadOnlyCollection<CommandsViewModel> _command;
        
        public MainWindowViewModel()
        {
            _workSpaces = new ObservableCollection<WorkspaceViewModel>();
            ShowHomePage();
            
        }

        public ReadOnlyCollection<CommandsViewModel> Commands
        {
            get
            {
                if (_command==null)
                {
                    List<CommandsViewModel> cmds = this.CreateCommands();
                    _command = new ReadOnlyCollection<CommandsViewModel>(cmds);
                }
                return _command;
            }
        }
                
        
        public ObservableCollection<WorkspaceViewModel> WorkSpaces
        {
            get
            {
                if (_workSpaces == null)
                {
                    _workSpaces = new ObservableCollection<WorkspaceViewModel>();
                    _workSpaces.CollectionChanged += this.OnWorkSpacesCollectionChanged;
                        
                }

                return _workSpaces;
            }
            
        }

        private void OnWorkSpacesCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            if (e.NewItems != null && e.NewItems.Count != 0)
            {
                foreach (WorkspaceViewModel workspace in e.NewItems)
                    workspace.RequestClose += this.OnWorkspaceRequestClose;
            }

            if (e.OldItems != null && e.OldItems.Count != 0)
            {
                foreach (WorkspaceViewModel workspace in e.OldItems)
                    workspace.RequestClose -= this.OnWorkspaceRequestClose;
            }
        }

        List<CommandsViewModel> CreateCommands()
        {
            return
                new List<CommandsViewModel>
                    { 
                        new CommandsViewModel("Create New Employee", 
                            new MVVMRoutedCommand((param) => this.CreateNewEmployee(),(param)=>this.CanCreateEmployee())),
                        new CommandsViewModel("Show All Employees",
                            new MVVMRoutedCommand((param) => this.ShowAllemployees(), (param) => this.CanShowAllEmployees())),
                        new CommandsViewModel("Show Home Page",
                            new MVVMRoutedCommand((param) => this.ShowHomePage(), (param) => this.CanShowHomePage()))
                    };
        }

        private void OnWorkspaceRequestClose(object sender, EventArgs e)
        {
            WorkspaceViewModel workspace = sender as WorkspaceViewModel;
            workspace.Dispose();
            this.WorkSpaces.Remove(workspace);
        }

        private void SetActiveWorkspace(WorkspaceViewModel workspace)
        {
            ICollectionView collectionView;
            collectionView = CollectionViewSource.GetDefaultView(this.WorkSpaces);
            collectionView.MoveCurrentTo(workspace);
            
        }
        private void CreateNewEmployee()
        {
            EmployeeViewModel empVM = new EmployeeViewModel();
            this.WorkSpaces.Add(empVM);
            SetActiveWorkspace(empVM);
        }

        private bool CanCreateEmployee()
        {
            return true;
        }

        private void ShowAllemployees()
        {
            EmployeeListViewModel vm = new EmployeeListViewModel();
            this.WorkSpaces.Add(vm);
            SetActiveWorkspace(vm);
        }

        private bool CanShowAllEmployees()
        {
            return true;
        }
        private void ShowHomePage()
        {
            HomePageViewModel hpvm = new HomePageViewModel();
            this.WorkSpaces.Add(hpvm);
            SetActiveWorkspace(hpvm);
        }
        private bool CanShowHomePage()
        {
            return true;
        }
    }
}
